package xcmg.device.web.sale;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.sale.SalesBillService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.sale.SalesBillVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Arrays;

/**
 * Created by wrg on 2019/4/22.
 */
@RestController
@RequestMapping("/salesBill")
@Api("销售单入口")
public class SalesBillController extends BasicController {
    @Autowired
    private SalesBillService salesBillService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增销售单信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody SalesBillVO salesBillVO) throws Exception {
        if (null == salesBillVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try{
            FunctionResult<String> result = salesBillService.add(salesBillVO, getUserId(), getOrgId());
            return actionResult(result.getCode(),result.getValue());
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(),e.getMessage());
        }
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除销售单信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult delete(@RequestBody String[] ids) throws Exception {
        if (ids == null || ids.length == 0) {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(salesBillService.delete(Arrays.asList(ids),getOrgId()));
    }

    @PostMapping(value = "/list")
    @ApiOperation("获取销售单列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<SalesBillVO> condition) throws Exception {
        SalesBillVO salesBillVO = condition.getFilters();
        if (null == salesBillVO) {
            salesBillVO = new SalesBillVO();
        }
        salesBillVO.setOrgId(getOrgId());
        condition.setFilters(salesBillVO);
        PageCommonVO pageCommonVO = salesBillService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @PostMapping(value = "/update")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("修改销售单信息")
    public ActionResult update(@RequestBody SalesBillVO salesBillVO) throws Exception {
        if (salesBillVO == null || salesBillVO.getDetailVOList() == null || salesBillVO.getDetailVOList().isEmpty()) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try{
            FunctionResult<String> functionResult = salesBillService.update(salesBillVO, getUserId(), getOrgId());
            return actionResult(functionResult.getCode(),functionResult.getValue());
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(),e.getMessage());
        }
    }

    @GetMapping(value = "/detail")
    @ApiOperation("获取销售单详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult detail(@RequestParam("id") String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            return actionResult(ErrorCode.IllegalArument);
        }

        return actionValueResult(salesBillService.detail(id));
    }

    @PostMapping(value = "/export")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("根据勾选数据导出")
    public void export(String[] ids) throws Exception {
        salesBillService.export(Arrays.asList(ids));
    }

    @PostMapping(value = "/exportAll")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("导出所有数据")
    public void exportAll(SalesBillVO condition) throws Exception {
        condition.setOrgId(getOrgId());
        salesBillService.exportAll(condition);
    }

    @PostMapping(value = "/salasOutBillList")
    @ApiOperation("获取销售单列表,用于销售出库选择销售单")
    @FastMappingInfo(needLogin = true)
    public ActionResult salasOutBillList(@RequestBody SearchCommonVO<SalesBillVO> condition) throws Exception {
        SalesBillVO salesBillVO = condition.getFilters();
        if (null == salesBillVO) {
            salesBillVO = new SalesBillVO();
        }
        salesBillVO.setOrgId(getOrgId());
        condition.setFilters(salesBillVO);
        PageCommonVO pageCommonVO = salesBillService.salasOutBillList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @GetMapping(value = "/findDeliveryNum")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("获取库存占用数量")
    public ActionResult findByDeliveryNum(@RequestParam("salesBillNo") String salesBillNo,
                                          @RequestParam("warehouseCode") String warehouseCode) throws Exception {
        if (StringUtils.isEmpty(salesBillNo) || StringUtils.isEmpty(warehouseCode)) {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(ErrorCode.Success, salesBillService.findDeliveryNum(salesBillNo, warehouseCode, getOrgId()));
    }

    @PostMapping(value = "/download")
    @ApiOperation("下载零件批量导入模板")
    @FastMappingInfo(needLogin = true)
    public void download() throws Exception {
        salesBillService.download();
    }

    @GetMapping(value = "/addClaimOrdNo")
    @ApiOperation("补充索赔单号")
    @FastMappingInfo(needLogin = true)
    public ActionResult addClaimOrdNo(@RequestParam("claimOrderNo") String claimOrderNo, @RequestParam("id") String id) {
        try {
            salesBillService.addClaimOrdNo(claimOrderNo, id);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(ErrorCode.Success);
    }

    @ResponseBody
    @PostMapping(value = "/import")
    @FastMappingInfo(needLogin = true)
    public ActionResult importExcel(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        FunctionResult<String> result = new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {
            InputStream in = file.getInputStream();
            result = salesBillService.importExcel(in, getOrgId());
        }
        return actionResult(result.getCode(), result.getValue());
    }

    @PostMapping(value = "/autoAdd")
    @ApiOperation(value = "自动新增销售单信息-进出口")
    @FastMappingInfo(needLogin = true)
    public ActionResult autoAdd(@RequestBody SalesBillVO salesBillVO) throws Exception {
        if (null == salesBillVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try{
            ErrorCode result = salesBillService.autoAdd(salesBillVO, getUserId(), getOrgId());
            return actionValueResult(result.getCode());
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(),e.getMessage());
        }
    }
}
